﻿using System;
using System.Collections.Generic;
using System.Text;
using Pixysoft.Framework.Reflection;
using Pixysoft.Weblications.Sango.Dao;
using Pixysoft.Weblications.Sango.Formular;
using Pixysoft.Weblications.Sango.Constant;
using Pixysoft.Weblications.Sango.Protocol.player;
using Pixysoft.Weblications.Sango.Protocol;
using Pixysoft.Weblications.Sango.Protocol.party;

namespace Pixysoft.Weblications.Sango.Command.party
{
    partial class SangoPartyHandler
    {
        public party_create party_create(string partyname)
        {
            string userCode = SangoManager.Authentication();

            party_create response = ReflectionManager.CreatePojo<party_create>();
            response.Succeed = false;

            if (string.IsNullOrEmpty(partyname))
            {
                response.Message = "丢失军团名";
                return response;
            }

            Entities.Usr_Partymember oldmember = PartyDao.tryGetActiveMember(userCode);

            if (oldmember != null)
            {
                response.Message = "已经加入了军团";
                return response;
            }

            if (PartyDao.tryGetPartyByName(partyname) != null)
            {
                response.Message = "军团名存在";
                return response;
            }


            Entities.Usr_Profile profile = UsrProfileDao.getUsrProfile(userCode);
            Entities.Usr_Property property = UsrPropertyDao.getProperty(userCode);

            long cashvalue = PartyFormular.default_cashvalue_createparty;

            if (!UsrProfileDao.tryDecreaseCashvalue(profile, cashvalue))
            {
                response.Message = "银币不足";
                return response;
            }

            string logoName = partyname.Substring(0, 1);

            if (!PartyFormular.isValidPartyName(partyname))
            {
                response.Message = "军团名非法";
                return response;
            }

            // 数据库

            IEntityNoebeTransaction command = SettingHelper.EntityTransaction;

            command.Begin(delegate()
            {
                Entities.Usr_Party party = PartyDao.createParty(profile, partyname, logoName);
                command.Insert(party);

                Entities.Usr_Partymission mission = PartyDao.createMission(party, PtyMissionTypecodeCollection.LEVELUP);
                command.Insert(mission);

                Entities.Usr_Partymember member = PartyDao.createPrecidentMember(
                    profile, property, party, mission);
                command.Insert(member);

                command.Update(profile);

                command.Commit();

                response.member = PartyFacade.ToPartyMember(profile, party, member);
            });

            response.profile = UsrProfileFacade.ToProfile(profile);
            response.Succeed = true;
            return response;
        }


    }
    public interface party_create : ISangoResponse
    {
        player_profile profile { get;set;}

        party_member member { get;set;}
    }
}
